package com.csxy.six.controller;

import com.csxy.six.R2;
import com.csxy.six.entity.User;
import com.csxy.six.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/auth")
@Tag(name = "认证管理", description = "用户认证相关接口")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private ObjectMapper objectMapper; // 用于JSON解析

    @PostMapping(value = "/register", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "学生注册", description = "注册新学生账号")
    public R2<User> register(
            @Parameter(description = "用户注册信息（JSON字符串）")
            @RequestParam(value = "user", required = false) String userJson,

            @Parameter(description = "头像图片文件")
            @RequestPart(value = "avatar", required = false) MultipartFile avatarFile) {

        User user = null;

        // 解析JSON用户信息
        if (StringUtils.hasText(userJson)) {
            try {
                user = objectMapper.readValue(userJson, User.class);
            } catch (IOException e) {
                return createErrorResponse(400, "用户信息格式错误");
            }
        }

        // 参数校验
        if (user == null) {
            return createErrorResponse(400, "用户信息不能为空");
        }
        if (!StringUtils.hasText(user.getPassword())) {
            return createErrorResponse(400, "密码不能为空");
        }

        return userService.register(user, avatarFile);
    }

    @PostMapping(value = "/registerJson", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "学生注册(JSON)", description = "注册新学生账号(纯JSON)")
    public R2<User> registerJson(
            @Parameter(description = "用户注册信息")
            @RequestBody User user) {

        // 参数校验
        if (user == null) {
            return createErrorResponse(400, "用户信息不能为空");
        }
        if (!StringUtils.hasText(user.getPassword())) {
            return createErrorResponse(400, "密码不能为空");
        }

        return userService.register(user, null);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "学生/教师登录")
    public R2<User> login(
            @Parameter(description = "用户名") @RequestParam String username,
            @Parameter(description = "密码") @RequestParam String password) {
        return userService.login(username, password);
    }

    /**
     * 创建错误响应（兼容 R2 类的私有构造函数）
     */
    private R2<User> createErrorResponse(int code, String message) {
        R2<User> response = R2.ok(null);
        response.setSuccess(false);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }
}